package com.bbtShop.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bbtShop.core.model.*;
import com.bbtShop.dao.ProductRecodeMapper;
import com.bbtShop.dao.SalesIndexViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 * @author : manke
 * @Description
 * @Date: 2020/10/25 21:25
 */
@Service
public class IndexService {
    @Autowired
    private SalesIndexViewMapper salesIndexViewMapper;
    @Autowired
    private ProductRecodeMapper productRecodeMapper;

    public Map<String, String> getIndexSales() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String format = simpleDateFormat.format(new Date());

        LambdaQueryWrapper<SalesIndexView> nowSalesIndexViewLambdaQueryWrapper = new LambdaQueryWrapper<>();
        nowSalesIndexViewLambdaQueryWrapper.like(SalesIndexView::getDate, format);
        BigDecimal nowTotalPrice = BigDecimal.ZERO;
        Integer nowTotalNum = 0;
        BigDecimal preTotalPrice = BigDecimal.ZERO;
        Integer preTotalNum = 0;
        List<SalesIndexView> nowSalesIndexViews = salesIndexViewMapper.selectList(nowSalesIndexViewLambdaQueryWrapper);
        for (SalesIndexView salesIndexView : nowSalesIndexViews) {
            nowTotalPrice = nowTotalPrice.add(salesIndexView.getTotalPrice());
            nowTotalNum = nowTotalNum + salesIndexView.getTotalNum();
        }
        String pre = subMonth(new Date());
        LambdaQueryWrapper<SalesIndexView> preSalesIndexViewLambdaQueryWrapper = new LambdaQueryWrapper<>();
        preSalesIndexViewLambdaQueryWrapper.like(SalesIndexView::getDate, pre);
        List<SalesIndexView> preSalesIndexViews = salesIndexViewMapper.selectList(preSalesIndexViewLambdaQueryWrapper);
        for (SalesIndexView salesIndexView : preSalesIndexViews) {
            preTotalPrice = preTotalPrice.add(salesIndexView.getTotalPrice());
            preTotalNum = preTotalNum + salesIndexView.getTotalNum();
        }
        HashMap<String, String> map = new HashMap<>();
        map.put("nowTotalPrice", nowTotalPrice.toString());
        map.put("nowTotalNum", nowTotalNum.toString());
        map.put("preTotalPrice", preTotalPrice.toString());
        map.put("preTotalNum", preTotalNum.toString());
        return map;

    }


    private String subMonth(Date dt) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, -1);
        Date dt1 = rightNow.getTime();
        return sdf.format(dt1);
    }

    public List<ProductSalesTopDto> getSalesTop() {
        List<ProductSalesTopDto> productSales = getProductTop();
        //计算总数
        int totalNum = 0;
        for (ProductSalesTopDto productSale : productSales) {
            totalNum = totalNum + productSale.getSalesNum();
        }
        for (int i = 0; i < productSales.size(); i++) {
            ProductSalesTopDto productSalesTopDto = productSales.get(i);
            BigDecimal bigDecimal = new BigDecimal(totalNum);
            Integer salesNum = productSalesTopDto.getSalesNum();
            BigDecimal bigDecimal1 = new BigDecimal(salesNum);
            BigDecimal divide = bigDecimal1.divide(bigDecimal, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
            productSalesTopDto.setPercentage(divide);
        }
        return productSales;
    }

    public HashMap<String, Object> getStatistics() {
        //按照最高销量选取四个要显示的产品id
        List<ProductSalesTopDto> productSales = getProductTop();
        Calendar c = Calendar.getInstance();

        //表格数据
        LinkedList<Object> objects = new LinkedList<>();
        for (ProductSalesTopDto productSale : productSales) {
            HashMap<String, Object> map = new HashMap<>();
            List<Integer> product = getProductWeek(productSale.getProductId());
            map.put("label", productSale.getProductName());
            map.put("data", product);
            objects.add(map);
        }
        //表头数据
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        LinkedList<String> linkedList = new LinkedList<>();
        for (int i = 0; i < 7; i++) {
            c.setTime(new Date());
            c.add(Calendar.DATE, - i);
            Date d = c.getTime();
            String date = df.format(d);
            linkedList.add(date);
        }
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("body", objects);
        objectObjectHashMap.put("foot", linkedList);

        return objectObjectHashMap;

    }


    /**
     * 根据产品id获取一周销量
     *
     * @return
     */
    private List<Integer> getProductWeek(Integer productId) {
        //按照最高销量选取四个要显示的产品id
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();

        //使用Date()
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 7; i++) {
            c.setTime(new Date());
            c.add(Calendar.DATE, - i);
            Date d = c.getTime();
            String preWeekDay = df.format(d);

            LambdaQueryWrapper<ProductRecode> productRecodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<ProductRecode> eq =
                    productRecodeLambdaQueryWrapper.eq(ProductRecode::getDate, preWeekDay)
                            .eq(ProductRecode::getProductId, productId);
            List<ProductRecode> productRecodes = productRecodeMapper.selectList(eq);
            if (productRecodes.size() < 1) {
                linkedList.add(0);
            } else {
                ProductRecode productRecode = productRecodes.get(0);
                linkedList.add(productRecode.getSalesNum());
            }

        }
        return linkedList;
    }


    public HashMap<String, Object> getStatisticsByYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<ProductSalesTopDto> productTop = getProductTop();
        LinkedList<Object> objects = new LinkedList<>();
        for (ProductSalesTopDto productSalesTopDto : productTop) {
            List<Integer> productYear = getProductYear(productSalesTopDto.getProductId());
            HashMap<String, Object> map = new HashMap<>();
            map.put("label", productSalesTopDto.getProductName());
            map.put("data", productYear);
            objects.add(map);
        }
        LinkedList<String> linkedList = new LinkedList<>();
        for (int i = 0; i < 6; i++) {
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(new Date());
            rightNow.add(Calendar.MONTH, -i);
            Date dt1 = rightNow.getTime();
            String format = sdf.format(dt1);
            linkedList.add(format);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("body", objects);
        map.put("foot", linkedList);
        return map;

    }

    private List<Integer> getProductYear(Integer productId) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 6; i++) {
            StatisticsDto statisticsDto = new StatisticsDto();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(new Date());
            rightNow.add(Calendar.MONTH, -i);
            Date dt1 = rightNow.getTime();
            String format = sdf.format(dt1);
            statisticsDto.setDate(format);
            statisticsDto.setProductId(productId);
            List<StatisticsYearDto> statisticsByYear = productRecodeMapper.getStatisticsByYear(statisticsDto);
            if (statisticsByYear.size() < 1) {
                linkedList.add(0);
            } else {
                StatisticsYearDto statisticsDto1 = statisticsByYear.get(0);
                linkedList.add(statisticsDto1.getSalesNum());
            }
        }
        return linkedList;


    }

    /**
     * 获取最近七天销售最高的产品
     *
     * @return
     */
    private List<ProductSalesTopDto> getProductTop() {
        //按照最高销量选取四个要显示的产品id
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //使用Date()
        String toDay = df.format(new Date());
        String preWeekDay = df.format(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7));
        TimeVo timeVo = new TimeVo();
        timeVo.setStartTime(preWeekDay);
        timeVo.setEndTime(toDay);
        return productRecodeMapper.getProductSales(timeVo);
    }

    public HashMap<String, Object> getDayTotal() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        LinkedList<String> labels = new LinkedList<>();

        LinkedList<Object> dates = new LinkedList<>();
        HashMap<String, List<Object>> stringListHashMap = new HashMap<>();
        stringListHashMap.put("data",dates);
        LinkedList<Object> dateBig = new LinkedList<>();
        dateBig.add(stringListHashMap);
        HashMap<String, Object> map = new HashMap<>();

        for (int i = 0; i < 30; i++) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, - i);
            Date d = c.getTime();
            String s = format.format(d);
            DayTotalDto dayTotalDto = new DayTotalDto();
            dayTotalDto.setDate(s);

            DayTotalDto dayTotal = productRecodeMapper.getDayTotal(dayTotalDto);
            if (dayTotal==null){
                dates.add("0");
            }else {
                dates.add(dayTotal.getSalesNum().toString());
            }
//            labels.add(s.split("-")[1]+"-"+s.split("-")[2]);
            labels.add(s.split("-")[2]);

        }
        map.put("body",dateBig);
        map.put("foot",labels);
        return map;


    }
}
